﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace MeizuSDK.Presentation
{
    /// <summary>
    /// 按钮控件
    /// </summary>
    public class Button : Control
    {
        #region DllImport
        [DllImport("MZFCWrapper.dll")]
        private static extern IntPtr UiButtonWrapperCtor(MouseEventFunc onLButtonDblClk,
            MouseEventFunc onLButtonDown, MouseEventFunc onLButtonUp, MouseEventFunc onMouseMove);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonWrapperSetButtonType(IntPtr pObj, int nType);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonWrapperGetButtonType(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonWrapperSetButtonMode(IntPtr pObj, int nMode);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonWrapperGetButtonMode(IntPtr pObj);

        [DllImport("MZFCWrapper.dll")]
        private static extern void UiButtonWrapperSetState(IntPtr pObj, int nState);

        [DllImport("MZFCWrapper.dll")]
        private static extern int UiButtonWrapperGetState(IntPtr pObj);
        #endregion

        #region 事件
        /// <summary>
        /// 当按钮被点击后触发此事件
        /// </summary>
        public event EventHandler Click;
        /// <summary>
        /// 当按钮状态被改变后触发此事件
        /// </summary>
        public event EventHandler<StateEventArgs> StateChanged;
        #endregion

        #region 常量
        /// <summary>
        /// 获取按钮默认宽度
        /// </summary>
        public const int WIDTH = MzMetrics.MZM_WIDTH_BUTTON_GRAY_TWO;
        /// <summary>
        /// 获取按钮默认高度（默认100）
        /// </summary>
        public const int HEIGHT = 100;// MzMetrics.MZM_HEIGHT_BUTTONEX;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取或设置按钮类型
        /// </summary>
        public Type ButtonType
        {
            get
            {
                return (Type)UiButtonWrapperGetButtonType(Handle);
            }
            set
            {
                UiButtonWrapperSetButtonType(Handle, (int)value);
            }
        }
        /// <summary>
        /// 获取或设置按钮当前模式
        /// </summary>
        public Mode ButtonMode
        {
            get
            {
                return (Mode)UiButtonWrapperGetButtonMode(Handle);
            }
            set
            {
                UiButtonWrapperSetButtonMode(Handle, (int)value);
            }
        }
        /// <summary>
        /// 获取或设置按钮当前状态
        /// </summary>
        public State ButtonState
        {
            get
            {
                return (State)UiButtonWrapperGetState(Handle);
            }
            set
            {
                UiButtonWrapperSetState(Handle, (int)value);
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造按钮对象
        /// </summary>
        public Button()
            : this(string.Empty)
        {

        }
        /// <summary>
        /// 构造按钮对象
        /// </summary>
        /// <param name="text"></param>
        public Button(string text)
        {
            Handle = UiButtonWrapperCtor(LButtonDbClkDelegate, LButtonDownDelegate,
                LButtonUpDelegate, MouseMoveDelegate);
            SetID();
            Size = new Size(WIDTH, HEIGHT);
            Text = text;
        }
        #endregion

        #region 函数
        /// <summary>
        /// 截获按钮事件
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        internal override void OnMzCommand(uint wParam, uint lParam)
        {
            OnClick(EventArgs.Empty);
        }
        /// <summary>
        /// 引发 LButtonUp 事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLButtonUp(MouseEventArgs e)
        {
            base.OnLButtonUp(e);
        }
        /// <summary>
        /// 引发 Click 事件
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnClick(EventArgs e)
        {
            if (ButtonMode == Mode.Hold)
                OnStateChanged((State)UiButtonWrapperGetState(Handle));

            if (Click != null)
                Click(this, e);
        }
        /// <summary>
        /// 引发 StateChange 事件
        /// </summary>
        /// <param name="state"></param>
        protected virtual void OnStateChanged(State state)
        {
            if (StateChanged != null)
                StateChanged(this, new StateEventArgs(state));
        }
        #endregion

        /// <summary>
        /// 按钮类型
        /// </summary>
        public enum Type : int
        {
            Default = MzControlDefs.MZC_BUTTON_DEFAULT,
            Green = MzControlDefs.MZC_BUTTON_GREEN,
            LineTopBottom = MzControlDefs.MZC_BUTTON_LINE_TOPBOTTOM,
            Select = MzControlDefs.MZC_BUTTON_SELECT,
            Switch = MzControlDefs.MZC_BUTTON_SWITCH,
        }
        /// <summary>
        /// 
        /// </summary>
        public enum Mode : int
        {
            Normal = MzControlDefs.MZC_BUTTON_MODE_NORMAL,
            Hold = MzControlDefs.MZC_BUTTON_MODE_HOLD,
        }
        /// <summary>
        /// 按钮状态
        /// </summary>
        public enum State : int
        {
            Normal = MzControlDefs.MZCS_BUTTON_NORMAL,
            Pressed = MzControlDefs.MZCS_BUTTON_PRESSED,

            Off = MzControlDefs.MZCS_BUTTON_NORMAL,
            On = MzControlDefs.MZCS_BUTTON_PRESSED,
        }

        /// <summary>
        /// StateChanged事件参数
        /// </summary>
        public class StateEventArgs : EventArgs
        {
            /// <summary>
            /// 构造StateChanged事件参数
            /// </summary>
            /// <param name="state"></param>
            public StateEventArgs(State state)
            {
                State = state;
            }
            /// <summary>
            /// 获取被改变后的按钮状态
            /// </summary>
            public State State
            {
                get;
                private set;
            }
        }
    }
}
